package diaodu.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.xkh.xkh.MyApplication;
import com.xkh.xkh.R;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import diaodu.dto.WaterDispatch;
import inter.Download;
import inter.DownloadInfo;
import managers.UIHandler;
import managers.VolleySingleton;
import shuiyuqing.model.BasicInfoModel;
import shuiyuqing.network.beans.IrrBAWaterGate;
import utils.TimeUtil;

/**
 * Created by Administrator on 2017/6/4.
 */

public class DealDispatchActivity extends UIHandler.BaseActivity {

    WaterDispatch dispatch;

    TextView dispatchNum;
    TextView sendName;
    TextView time;
    TextView receiveName;
    TextView gateName;
    TextView status;
    TextView dispatchControl;
    TextView upperLevel;
    TextView downLevel;

    TextView open1;
    TextView change1;

    TextView open2;
    TextView change2;

    TextView open3;
    TextView change3;

    TextView open4;
    TextView change4;

    TextView open5;
    TextView change5;

    Button deal;
    Button back;
    private View.OnClickListener itemClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view == deal) {
                deal();
            }
        }
    };

    // 处理请求
    private void deal() {
        if (!MyApplication.getOwnUser().getUser_RealName().equals(dispatch.getReceiveName()))
        {
            Toast.makeText(MyApplication.share(),"您不能代他人处理",Toast.LENGTH_SHORT).show();
            return;
        }

        new AlertDialog.Builder(this).setTitle("处理").setMessage("是否处理").setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dealRequest();
            }
        }).show();
    }

    // 进行http请求
    private void dealRequest() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/guanqu/ChangeWaterDispatch";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (!response.startsWith("{")) {
                            return;
                        }

                        Download download = new Gson().fromJson(response,Download.class);
                        if (download.getStatus() == DownloadInfo.SUCCESS)
                        {
                            Toast.makeText(MyApplication.share(),"处理成功",Toast.LENGTH_SHORT).show();
                            DiaoDuShowActivity activity = (DiaoDuShowActivity) UIHandler.uiHandler.getActivityByName("DiaoDuShowActivity");
                            int position = getIntent().getIntExtra("position",0);
                            activity.removeAndNotify(position);
                            DealDispatchActivity.this.finish();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("id", dispatch.getId() + "");
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dispatch = (WaterDispatch) getIntent().getSerializableExtra("dispatch");

        setContentView(R.layout.activity_dealdispatch);


        dispatchNum = (TextView) findViewById(R.id.dispatchNum);
        dispatchNum.setText(dispatch.getWaterNum());
        sendName = (TextView) findViewById(R.id.sendName);
        sendName.setText(dispatch.getSendName());
        time = (TextView) findViewById(R.id.time);
        time.setText(TimeUtil.getTimeByDate2(dispatch.getSendTime().getTime()));
        receiveName = (TextView) findViewById(R.id.receiveName);
        receiveName.setText(dispatch.getReceiveName());
        gateName = (TextView) findViewById(R.id.gateName);
        IrrBAWaterGate gate = BasicInfoModel.getwaterGateByStadia(dispatch.getWatergateCode());
        gateName.setText(gate.getWatergateName());
        status = (TextView) findViewById(R.id.status);
        status.setText(dispatch.getState() == 1 ? "未处理" : "已处理");
        dispatchControl = (TextView) findViewById(R.id.dispatchControl);
        dispatchControl.setText(dispatch.getHeightChange());
        upperLevel = (TextView) findViewById(R.id.upperLevel);
        upperLevel.setText(dispatch.getHeightUp().setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "");
        downLevel = (TextView) findViewById(R.id.downLevel);
        downLevel.setText(dispatch.getHeightDown().setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "");


        open1 = (TextView) findViewById(R.id.open1);
        change1 = (TextView) findViewById(R.id.change1);

        open2 = (TextView) findViewById(R.id.open2);
        change2 = (TextView) findViewById(R.id.change2);

        open3 = (TextView) findViewById(R.id.open3);
        change3 = (TextView) findViewById(R.id.change3);

        open4 = (TextView) findViewById(R.id.open4);
        change4 = (TextView) findViewById(R.id.change4);

        open5 = (TextView) findViewById(R.id.open5);
        change5 = (TextView) findViewById(R.id.change5);

        String open = dispatch.getOpenDegree();
        String change = dispatch.getHeightChange();

        String[] opens = open.split("、");
        String[] changes = change.split("、");

        TextView[] opensTx = new TextView[5];
        TextView[] changeTx = new TextView[5];
        opensTx[0] = open1;
        changeTx[0] = change1;
        opensTx[1] = open2;
        changeTx[1] = change2;
        opensTx[2] = open3;
        changeTx[2] = change3;
        opensTx[3] = open4;
        changeTx[3] = change4;
        opensTx[4] = open5;
        changeTx[4] = change5;

        int i = 0;
        for (; i < opens.length; i++) {
//            String openi = opens[i].split(":")[1];
            opensTx[i].setText(opens[i].split(":")[1]);
        }
        for (; i < 5; i++) {
            opensTx[i].setText("");
        }

        int j = 0;
        for (; j < changes.length; j++) {
            changeTx[j].setText(changes[j].split(":")[1]);
        }
        for (; j < 5; j++) {
            changeTx[j].setText("");
        }

        deal = (Button) findViewById(R.id.deal);


        deal.setOnClickListener(itemClick);


        if (dispatch.getState() == 2) {
            deal.setText("已处理");
            deal.setEnabled(false);
        }

    }
}
